package com.hanson.api;

import com.alibaba.fastjson.JSON;
import com.hanson.Const;
import com.hanson.common.bean.ResultMessage;
import com.hanson.common.service.RedisService;
import com.hanson.common.util.QrCodeUtil;
import com.hanson.entity.UserBean;
import com.hanson.mapper.UserBeanMapper;
import com.hanson.service.MergeUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/api/sys")
@Api(tags = "系统管理")
public class SysApiController {

    @Autowired
    private RedisService redisService;
    @Autowired
    private UserBeanMapper userBeanMapper;
    @Autowired
    private MergeUserService mergeUserService;
    @Autowired
    private QrCodeUtil qrCodeUtil;

    @ApiOperation(value = "删除key", notes = "删除key")
    @RequestMapping(value = "/del/{key}", method = RequestMethod.POST)
    public ResultMessage del(@PathVariable String key) {
        ResultMessage resultMessage = new ResultMessage();
        Set<String> keys = redisService.keys(key);
        log.info("keys:{}", JSON.toJSONString(key));
        for (String redisKey : keys) {
            redisService.del(redisKey);
        }
        resultMessage.setMsg("成功！");
        return resultMessage;
    }

    @ApiOperation(value = "QRCode生成", notes = "QRCode生成")
    @RequestMapping(value = "/qrCode", method = RequestMethod.POST)
    public ResultMessage qrCodes() {
        ResultMessage resultMessage = new ResultMessage();
        List<UserBean> userBeanList = userBeanMapper.getAll();
        if (CollectionUtils.isNotEmpty(userBeanList)) {
            for (UserBean userBean : userBeanList) {
                if (userBean.getIsSpread() == 1) {
                    try {
                        String miniUrl = qrCodeUtil.createQrCode(Const.CLIENT_MINI, userBean.getId());
                        String h5Url = qrCodeUtil.createQrCode(Const.CLIENT_H5, userBean.getId());
                        userBean.setSignDate(new Date());
                        userBean.setUserSign(miniUrl + "," + h5Url);
                        userBeanMapper.updateByPrimaryKeySelective(userBean);
                        redisService.del(String.format(Const.USER_REDIS_KEY, userBean.getOpenId()));
                    } catch (Exception e) {
                        e.printStackTrace();
                        log.error("QRCode生成报错 userId=" + userBean.getId(), e);
                    }
                }
            }
        }
        resultMessage.setMsg("成功！");
        return resultMessage;
    }

    @ApiOperation(value = "合并用户", notes = "合并用户")
    @RequestMapping(value = "/mergeUser", method = RequestMethod.POST)
    public ResultMessage mergeUser() {
        ResultMessage resultMessage = new ResultMessage();
        List<UserBean> userBeanList = userBeanMapper.getAll();
        if (CollectionUtils.isNotEmpty(userBeanList)) {
            for (UserBean userBean : userBeanList) {
                if (StringUtils.isNotEmpty(userBean.getOpenId()) && StringUtils.isNotEmpty(userBean.getMpOpenId())) {
                    //更新公众号用户数据
                    mergeUserService.mergeUser(userBean.getOpenId(), userBean.getMpOpenId(), null);
                }
            }
        }
        resultMessage.setMsg("成功！");
        return resultMessage;
    }
}
